package com.bee.oa.web.dwr;

import java.util.List;
import java.util.UUID;

import com.bee.base.util.DateUtil;
import com.bee.base.web.dwr.BaseDwr;
import com.bee.oa.domain.sm.Resource;
import com.bee.oa.domain.sm.X;
import com.bee.oa.service.sm.AccountService;
import com.bee.oa.service.sm.DictionaryItemService;
import com.bee.oa.service.sm.DictionaryService;
import com.bee.oa.service.sm.GroupService;
import com.bee.oa.service.sm.OrganizationService;
import com.bee.oa.service.sm.ParameterService;
import com.bee.oa.service.sm.PostService;
import com.bee.oa.service.sm.ResourceService;
import com.bee.oa.service.sm.RoleService;
import com.bee.oa.service.sm.UserService;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

@SuppressWarnings("unused")
public class SmDwr extends BaseDwr {

	private static final long serialVersionUID = 6606735431982316535L;

	private AccountService accountService;
	private DictionaryService dictionaryService;
	private DictionaryItemService dictionaryItemService;
	private GroupService groupService;
	private OrganizationService organizationService;
	private ParameterService parameterService;
	private PostService postService;
	private ResourceService resourceService;
	private RoleService roleService;
	private UserService userService;

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public void setDictionaryItemService(DictionaryItemService dictionaryItemService) {
		this.dictionaryItemService = dictionaryItemService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public void setPostService(PostService postService) {
		this.postService = postService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<Resource> loadAllResource() {
		return resourceService.find("from Resource ");
	}

	public void x() {
		// 初始化com的线程，非常重要！！使用结束后要调用 realease方法
		ComThread.InitSTA();

		// 实例化ActiveX组件对象：对word进行操作
		ActiveXComponent wrdCom = new ActiveXComponent("Word.Application");

		// 设置word不可见
		wrdCom.setProperty("Visible", new Variant(false));

		// 获取Dispatch的Documents对象
		Dispatch wrdDocs = wrdCom.getProperty("Documents").toDispatch();

		// 打开一个已经存在的文档
		Dispatch doc = Dispatch.call(wrdDocs, "Open", "C:\\Users\\Jerrold\\Desktop\\南川模板\\标准模板（上行文）.doc").toDispatch();

		// 获得当前word文档文本
		Dispatch docSelection = Dispatch.get(wrdCom, "Selection").toDispatch();

		// 从selection所在位置开始查询
		Dispatch find = Dispatch.call(docSelection, "Find").toDispatch();

		int i = 275;
		List<X> list = resourceService.findX("from X where STATUS = 1 and PARENT_SID = 0 order by ORDER_BY asc");
		for (X x : list) {
			// 设置要查找的内容
			Dispatch.put(find, "Text", "重庆市南川区XXXX");

			// 向前查找
			Dispatch.put(find, "Forward", "True");

			// 设置格式
			Dispatch.put(find, "Format", "True");

			// 大小写匹配
			Dispatch.put(find, "MatchCase", "True");

			// 全字匹配
			Dispatch.put(find, "MatchWholeWord", "True");

			// 查询
			Dispatch.call(find, "Execute").getBoolean();

			// 替换
			Dispatch.put(docSelection, "Text", x.getCNAME());

			// 另存为
			StringBuilder sb = new StringBuilder("UIE");
			sb.append(DateUtil.formatDate(DateUtil.getCurrentDate(), "yyyyMMddHHmmss"));
			sb.append(UUID.randomUUID().toString().replace("-", ""));
			Dispatch.call(doc, "SaveAs", new Variant("C:\\Users\\Jerrold\\Desktop\\南川模板\\" + x.getLOGIN_PAGE()
					+ "\\CFG_TEMPLATE\\" + sb.toString() + ".doc"));

			String tname = "发文模板（上行文）";

			System.out.println("INSERT INTO cfg_template VALUES(" + i + ",0,GETDATE(),null,1,null,1,1,'发文',1,'红头模板','"
					+ tname + "',999," + x.getSID() + "," + x.getSID() + ");");
			System.out.println("INSERT INTO sys_attachment VALUES (" + i + ",0,GETDATE(),null,null,1,1," + x.getSID()
					+ "," + x.getSID() + ",'CFG_TEMPLATE'," + i + ",'" + tname + ".doc','/uie-resource/"
					+ x.getLOGIN_PAGE() + "/CFG_TEMPLATE/" + sb.toString() + ".doc','application/msword',2,0,0,1);");
			i++;
		}

		// 保存关闭
		if (doc != null) {
			Dispatch.call(doc, "Save");
			Dispatch.call(doc, "Close", new Variant(true));
			doc = null;
		}

		// 关闭word文件
		wrdCom.invoke("Quit", new Variant[] {});

		// 释放com线程。根据jacob的帮助文档，com的线程回收不由java的垃圾回收器处理
		ComThread.Release();
	}

}
